home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / adv.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-09  |  13.5 KB  |  463 lines

  1. /*
  2.  
  3.     File: adv.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.   
  7.     This software is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.   
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.   
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  */
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include "types.h"
  28. #include "common.h"
  29. #include "lang.h"
  30. #include "intrface.h"
  31. #include "readpart.h"
  32. #include "fnctdsk.h"
  33. #include "testdisk.h"
  34. #include "fat.h"
  35. #include "ntfs.h"
  36. #include "adv.h"
  37.  
  38. void interface_adv(t_param_disk *disk_car, const int debug,const int dump_ind)
  39. {
  40.   int quit;
  41.   int offset=0;
  42.   int current_element_num=0;
  43.   int rewrite=1;
  44.   t_list_part *element;
  45.   t_list_part *list_part=read_part(disk_car, debug);
  46.   t_list_part *current_element=list_part;
  47.   ecrit_rapport("\nInterface Advanced\n");
  48.   for(element=list_part;element!=NULL;element=element->next)
  49.   {
  50.     aff_part_rapport(disk_car,element->part);
  51.   }
  52.   do
  53.   {
  54.     static struct MenuItem menuAdv[]=
  55.     {
  56.       {'q',"Quit","Return to main menu"},
  57.       {'b',"Boot","Boot sector recovery"},
  58.       {0,NULL,NULL}
  59.     };
  60.     int opt_B=0;
  61.     int i;
  62.     int command;
  63.     if(rewrite!=0)
  64.     {
  65.       aff_copy(stdscr);
  66.       wmove(stdscr,4,0);
  67.       wdoprintf(stdscr,"%s",disk_car->description(disk_car));
  68. /*     mvwaddstr(stdscr,22,0,msg_PART_TYPE); */
  69. /*     mvwaddstr(stdscr,5,0,msg_CHOOSE_PART_TYPE); */
  70.       mvwaddstr(stdscr,6,0,msg_PART_HEADER2);
  71.       rewrite=0;
  72.     }
  73.     for(i=0,element=list_part;(element!=NULL) && (i<offset);element=element->next,i++);
  74.     for(i=offset;(element!=NULL) && ((i-offset)<INTER_ADV);i++,element=element->next)
  75.     {
  76.       wmove(stdscr,5+2+i-offset,0);
  77.       wclrtoeol(stdscr);    /* before addstr for BSD compatibility */
  78.       if(element==current_element)
  79.       {
  80.     wstandout(stdscr);
  81.     aff_part(stdscr,AFF_PART_ORDER,disk_car,element->part);
  82.     wstandend(stdscr);
  83.       } else
  84.       {
  85.     aff_part(stdscr,AFF_PART_ORDER,disk_car,element->part);
  86.       }
  87.     }
  88.     if(current_element!=NULL)
  89.     {
  90.       if(is_fat(current_element->part->part_type) ||
  91.       is_ntfs(current_element->part->part_type))
  92.     opt_B=1;
  93.     }
  94.     quit=0;
  95.     command = wmenuSelect(stdscr,INTER_ADV_Y, INTER_ADV_X, menuAdv, 8,
  96.     opt_B!=0?"bq":"q", MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, opt_B);
  97.     switch(command)
  98.     {
  99.       case MENU_UP:
  100.     if(current_element!=NULL)
  101.     {
  102.       if(current_element->prev!=NULL)
  103.       {
  104.         current_element=current_element->prev;
  105.         current_element_num--;
  106.       }
  107.       if(current_element_num<offset)
  108.         offset--;
  109.     }
  110.     break;
  111.       case MENU_DOWN:
  112.     if(current_element!=NULL)
  113.     {
  114.       if(current_element->next!=NULL)
  115.       {
  116.         current_element=current_element->next;
  117.         current_element_num++;
  118.       }
  119.       if(current_element_num>=offset+INTER_ADV)
  120.         offset++;
  121.     }
  122.     break;
  123.       case 'q':
  124.       case 'Q':
  125.     quit=1;
  126.     break;
  127.       case 'b':
  128.       case 'B':
  129.     if(current_element!=NULL)
  130.     {
  131.       if(is_fat32(current_element->part->part_type))
  132.       {
  133.         fat32_boot_sector(disk_car, current_element->part, debug, dump_ind);
  134.         rewrite=1;
  135.       }
  136.       else if(is_fat(current_element->part->part_type))
  137.       {
  138.         fat1x_boot_sector(disk_car, current_element->part, debug, dump_ind);
  139.         rewrite=1;
  140.       }
  141.       else if(is_ntfs(current_element->part->part_type))
  142.       {
  143.         ntfs_boot_sector(disk_car, current_element->part, debug, dump_ind);
  144.         rewrite=1;
  145.       }
  146.     }
  147.     break;
  148.     }
  149.   } while(quit==0);
  150.   delete_list_part(list_part);
  151. }
  152.  
  153. int fat1x_boot_sector(t_param_disk *disk_car, t_diskext *partition, const int debug, const int dump_ind)
  154. {
  155.   t_sector buffer_bs;
  156.   const char *options="DR";
  157.   int rescan=1;
  158.   while(1)
  159.   {
  160.     aff_buffer(BUFFER_RESET,"Q");
  161.     if(rescan==1)
  162.     {
  163.       aff_copy(stdscr);
  164.       wmove(stdscr,4,0);
  165.       wdoprintf(stdscr,"%s",disk_car->description(disk_car));
  166.       wmove(stdscr,5,0);
  167.       aff_part(stdscr,AFF_PART_ORDER,disk_car,partition);
  168.       ecrit_rapport("\nfat1x_boot_sector\n");
  169.       aff_part_rapport(disk_car,partition);
  170.       aff_buffer(BUFFER_ADD,"Boot sector\n");
  171.       if(disk_car->read(disk_car,1, &buffer_bs, partition->lba)!=0)
  172.       {
  173.     ecrit_rapport(msg_CHKFAT_RERR); return 1;
  174.       }
  175.       if(test_FAT(disk_car,(const struct fat_boot_sector *)buffer_bs,partition,debug,0)==0)
  176.       {
  177.     aff_buffer(BUFFER_ADD,"OK\n");
  178.       }
  179.       rescan=0;
  180.     }
  181.     /*  mvwaddstr(stdscr,23,0,msg_PART_TYPE); */
  182.     switch(aff_buffer(BUFFER_DISPLAY,options,stdscr))
  183.     {
  184.       case 0:
  185.     return 0;
  186.       case 3: /* R : rebuild boot sector */
  187.     rebuild_FAT_BS(disk_car,partition,debug,dump_ind,1);
  188.     rescan=1;
  189.     break;
  190.       case 4:
  191.     {
  192.       WINDOW *window=newwin(0,0,0,0);    /* full screen */
  193.       keypad(window, TRUE); /* Need it to get arrow key */
  194.       aff_copy(window);
  195.       wmove(window,4,0);
  196.       wdoprintf(window,"%s",disk_car->description(disk_car));
  197.       wmove(window,5,0);
  198.       aff_part(window,AFF_PART_ORDER,disk_car,partition);
  199.       ecrit_rapport("Boot sector\n");
  200.       mvwaddstr(window,6,0, "Boot sector");
  201.       dump(window,buffer_bs,SECTOR_SIZE);
  202.       delwin(window);
  203. #ifdef DJGPP
  204.       wredrawln(stdscr,0,stdscr->_maxy);    /* redrawwin def is boggus in pdcur24 */
  205. #else
  206.       redrawwin(stdscr);    /* stdscr has been corrupted by window */
  207. #endif
  208.     }
  209.     break;
  210.     }
  211.   }
  212. }
  213.  
  214. int fat32_boot_sector(t_param_disk *disk_car, t_diskext *partition, const int debug, const int dump_ind)
  215. {
  216.   unsigned char buffer_bs[3*SECTOR_SIZE];
  217.   unsigned char buffer_backup_bs[3*SECTOR_SIZE];
  218.   const char *options="";
  219.   int rescan=1;
  220.   while(1)
  221.   {
  222.     aff_buffer(BUFFER_RESET,"Q");
  223.     if(rescan==1)
  224.     {
  225.       int opt_over=0;
  226.       int opt_B=0;
  227.       int opt_O=0;
  228.       options="DR";
  229.       aff_copy(stdscr);
  230.       wmove(stdscr,4,0);
  231.       wdoprintf(stdscr,"%s",disk_car->description(disk_car));
  232.       wmove(stdscr,5,0);
  233.       aff_part(stdscr,AFF_PART_ORDER,disk_car,partition);
  234.       ecrit_rapport("\nfat32_boot_sector\n");
  235.       aff_part_rapport(disk_car,partition);
  236.       aff_buffer(BUFFER_ADD,"Boot sector\n");
  237.       if(disk_car->read(disk_car,3, &buffer_bs, partition->lba)!=0)
  238.       {
  239.     ecrit_rapport(msg_CHKFAT_RERR); return 1;
  240.       }
  241.       if(test_FAT(disk_car,(struct fat_boot_sector *)buffer_bs,partition,debug,0)==0)
  242.       {
  243.     aff_buffer(BUFFER_ADD,"OK\n");
  244.     opt_O=1;
  245.     opt_over=1;
  246.       }
  247.       aff_buffer(BUFFER_ADD,"\nBackup boot sector\n");
  248.       if(disk_car->read(disk_car,3, &buffer_backup_bs, partition->lba+6)!=0)
  249.       {
  250.     ecrit_rapport(msg_CHKFAT_RERR); return 1;
  251.       }
  252.       if(test_FAT(disk_car,(struct fat_boot_sector *)buffer_backup_bs,partition,debug,0)==0)
  253.       {
  254.     aff_buffer(BUFFER_ADD,"OK\n");
  255.     opt_B=1;
  256.     opt_over=1;
  257.       }
  258.       aff_buffer(BUFFER_ADD,"\n");
  259.       if((memcmp(buffer_bs,buffer_backup_bs,0x3E8)==0)&&(memcmp(&buffer_bs[0x3F0],&buffer_backup_bs[0x3F0],0x600-0x3F0))==0)
  260.       {
  261.     aff_buffer(BUFFER_ADD,"Sectors are identical.\n");
  262.     opt_over=0;
  263.       }
  264.       else
  265.       {
  266.     if(memcmp(buffer_bs,buffer_backup_bs,SECTOR_SIZE)!=0)
  267.       aff_buffer(BUFFER_ADD,"First sectors (Boot code and partition information) are not identical.\n");
  268.     if((memcmp(&buffer_bs[SECTOR_SIZE],&buffer_backup_bs[SECTOR_SIZE],0x1E8)!=0)||
  269.         (memcmp(&buffer_bs[SECTOR_SIZE+0x1F0],&buffer_backup_bs[SECTOR_SIZE+0x1F0],SECTOR_SIZE-0x1F0)!=0))
  270.       aff_buffer(BUFFER_ADD,"Second sectors (cluster information) are not identical.\n");
  271.     if(memcmp(&buffer_bs[2*SECTOR_SIZE],&buffer_backup_bs[2*SECTOR_SIZE],SECTOR_SIZE)!=0)
  272.       aff_buffer(BUFFER_ADD,"Third sectors (Second part of boot code) are not identical.\n");
  273.       }
  274.  
  275.       if(opt_over!=0)
  276.       {
  277.     if(opt_B!=0 && opt_O!=0)
  278.       options="DOBR";
  279.     else if(opt_B!=0)
  280.       options="DBR";
  281.     else if(opt_O!=0)
  282.       options="DOR";
  283.       }
  284.       rescan=0;
  285.     }
  286.     switch(aff_buffer(BUFFER_DISPLAY,options,stdscr))
  287.     {
  288.       case 0:
  289.     return 0;
  290.       case 1: /* O : copy original boot sector over backup boot */
  291.     if(ask_confirmation("Copy original FAT32 boot sector over backup boot, confirm ? (Y/N)")!=0)
  292.     {
  293.       ecrit_rapport("copy original boot sector over backup boot\n");
  294.       if(disk_car->write(disk_car,3, &buffer_bs, partition->lba+6)!=0)
  295.       {
  296.         display_message("Write error: Can't overwrite FAT32 backup boot sector\n");
  297.       }
  298.       rescan=1;
  299.     }
  300.     break;
  301.       case 2: /* B : copy backup boot sector over boot sector */
  302.     if(ask_confirmation("Copy backup FAT32 boot sector over boot sector, confirm ? (Y/N)")!=0)
  303.     {
  304.       ecrit_rapport("copy backup boot sector over boot sector\n");
  305.       if(disk_car->write(disk_car,3, &buffer_backup_bs, partition->lba)!=0)
  306.       {
  307.         display_message("Write error: Can't overwrite FAT32 boot sector\n");
  308.       }
  309.       rescan=1;
  310.     }
  311.     break;
  312.       case 3: /* R : rebuild boot sector */
  313.     rebuild_FAT_BS(disk_car,partition,debug,dump_ind,1);
  314.     rescan=1;
  315.     break;
  316.       case 4:
  317.     {
  318.       WINDOW *window=newwin(0,0,0,0);    /* full screen */
  319.       keypad(window, TRUE); /* Need it to get arrow key */
  320.       aff_copy(window);
  321.       wmove(window,4,0);
  322.       wdoprintf(window,"%s",disk_car->description(disk_car));
  323.       wmove(window,5,0);
  324.       aff_part(window,AFF_PART_ORDER,disk_car,partition);
  325.       ecrit_rapport("Boot sector                        Backup boot sector\n");
  326.       mvwaddstr(window,6,0, "Boot sector                        Backup boot sector");
  327.       dump2(window,buffer_bs,buffer_backup_bs,3*SECTOR_SIZE);
  328.       delwin(window);
  329. #ifdef DJGPP
  330.       wredrawln(stdscr,0,stdscr->_maxy);    /* redrawwin def is boggus in pdcur24 */
  331. #else
  332.       redrawwin(stdscr);    /* stdscr has been corrupted by window */
  333. #endif
  334.       dump_2fat_rapport((const struct fat_boot_sector*)&buffer_bs,(const struct fat_boot_sector*)&buffer_backup_bs,UP_FAT32);
  335.  
  336.     }
  337.     break;
  338.     }
  339.   }
  340. }
  341.  
  342. int ntfs_boot_sector(t_param_disk *disk_car, t_diskext *partition, const int debug, const int dump_ind)
  343. {
  344.   unsigned char buffer_bs[SECTOR_SIZE];
  345.   unsigned char buffer_backup_bs[SECTOR_SIZE];
  346.   const char *options="";
  347.   int rescan=1;
  348.   while(1)
  349.   {
  350.     aff_buffer(BUFFER_RESET,"Q");
  351.     if(rescan==1)
  352.     {
  353.       int opt_over=0;
  354.       int opt_B=0;
  355.       int opt_O=0;
  356.       options="D";
  357.       aff_copy(stdscr);
  358.       wmove(stdscr,4,0);
  359.       wdoprintf(stdscr,"%s",disk_car->description(disk_car));
  360.       wmove(stdscr,5,0);
  361.       aff_part(stdscr,AFF_PART_ORDER,disk_car,partition);
  362.       ecrit_rapport("\nntfs_boot_sector\n");
  363.       aff_part_rapport(disk_car,partition);
  364.       aff_buffer(BUFFER_ADD,"Boot sector\n");
  365.       if(disk_car->read(disk_car,1, &buffer_bs, partition->lba)!=0)
  366.       {
  367.     ecrit_rapport(msg_CHKFAT_RERR); return 1;
  368.       }
  369.       if(test_NTFS(disk_car,(struct ntfs_boot_sector*)buffer_bs,partition,debug,0)!=0)
  370.       {
  371.     aff_buffer(BUFFER_ADD,"OK\n");
  372.     opt_O=1;
  373.     opt_over=1;
  374.       }
  375.       aff_buffer(BUFFER_ADD,"\nBackup boot sector\n");
  376.       if(disk_car->read(disk_car,1, &buffer_backup_bs, partition->lba+partition->part_size-1)!=0)
  377.       {
  378.     ecrit_rapport(msg_CHKFAT_RERR); return 1;
  379.       }
  380.       if(test_NTFS(disk_car,(struct ntfs_boot_sector*)buffer_backup_bs,partition,debug,0)==0)
  381.       {
  382.     aff_buffer(BUFFER_ADD,"OK\n");
  383.     opt_B=1;
  384.     opt_over=1;
  385.       }
  386.       aff_buffer(BUFFER_ADD,"\n");
  387.       if(memcmp(buffer_bs,buffer_backup_bs,SECTOR_SIZE)==0)
  388.       {
  389.     dump_ntfs_rapport((const struct ntfs_boot_sector *)buffer_bs);
  390.     aff_buffer(BUFFER_ADD,"Sectors are identical.\n");
  391.     opt_over=0;
  392.       }
  393.       else
  394.       {
  395.     dump_2ntfs_rapport((const struct ntfs_boot_sector *)buffer_bs, (const struct ntfs_boot_sector *)buffer_backup_bs);
  396.     aff_buffer(BUFFER_ADD,"Sectors are not identical.\n");
  397.       }
  398.       if(opt_over!=0)
  399.       {
  400.     if(opt_B!=0 && opt_O!=0)
  401.       options="DOB";
  402.     else if(opt_B!=0)
  403.       options="DB";
  404.     else if(opt_O!=0)
  405.       options="DO";
  406.       }
  407.       rescan=0;
  408.     }
  409.     switch(aff_buffer(BUFFER_DISPLAY,options,stdscr))
  410.     {
  411.       case 0:
  412.     return 0;
  413.       case 1: /* O : copy original boot sector over backup boot */
  414.     if(ask_confirmation("Copy original NTFS boot sector over backup boot, confirm ? (Y/N)")!=0)
  415.     {
  416.       ecrit_rapport("copy original boot sector over backup boot\n");
  417.       if(disk_car->write(disk_car,3, &buffer_bs, partition->lba+6)!=0)
  418.       {
  419.         display_message("Write error: Can't overwrite NTFS backup boot sector\n");
  420.       }
  421.       rescan=1;
  422.     }
  423.     break;
  424.       case 2: /* B : copy backup boot sector over boot sector */
  425.     if(ask_confirmation("Copy backup NTFS boot sector over boot sector, confirm ? (Y/N)")!=0)
  426.     {
  427.       ecrit_rapport("copy backup boot sector over boot sector\n");
  428.       if(disk_car->write(disk_car,3, &buffer_backup_bs, partition->lba)!=0)
  429.       {
  430.         display_message("Write error: Can't overwrite NTFS boot sector\n");
  431.       }
  432.       rescan=1;
  433.     }
  434.     break;
  435.       case 3: /* R : rebuild boot sector */
  436.     rebuild_NTFS_BS(disk_car,partition,debug,dump_ind,1);
  437.     rescan=1;
  438.     break;
  439.       case 4:
  440.     {
  441.       WINDOW *window=newwin(0,0,0,0);    /* full screen */
  442.       keypad(window, TRUE); /* Need it to get arrow key */
  443.       aff_copy(window);
  444.       wmove(window,4,0);
  445.       wdoprintf(window,"%s",disk_car->description(disk_car));
  446.       wmove(window,5,0);
  447.       aff_part(window,AFF_PART_ORDER,disk_car,partition);
  448.       ecrit_rapport("Boot sector                        Backup boot sector\n");
  449.       mvwaddstr(window,6,0, "Boot sector                        Backup boot sector");
  450.       dump2(window,buffer_bs,buffer_backup_bs,SECTOR_SIZE);
  451.       delwin(window);
  452. #ifdef DJGPP
  453.       wredrawln(stdscr,0,stdscr->_maxy);    /* redrawwin def is boggus in pdcur24 */
  454. #else
  455.       redrawwin(stdscr);    /* stdscr has been corrupted by window */
  456. #endif
  457.     }
  458.     break;
  459.     }
  460.   }
  461. }
  462.  
  463.